package com.wangshuqi.boot.config;

import com.wangshuqi.boot.quartz.MyFirstJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail firstJobDetail() {
        JobDetail jobDetail = JobBuilder.newJob(MyFirstJob.class)
                .withIdentity("firstJob", "TestJob") //可以给该JobDetail起一个id和组
                //每个JobDetail内都有一个Map，包含了关联到这个Job的数据，在Job类中可以通过context获取
                .usingJobData("msg", "My first quartz job")
                .storeDurably() //即使没有Trigger关联时，也不需要删除该JobDetail
                .build();
        return jobDetail;
    }

    @Bean
    public Trigger firstTrigger() {
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/1 * * * * ?");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .forJob(firstJobDetail()) // 关联上述的JobDetail
                .withIdentity("firstTriggerService") // 给Trigger起个名字
                .withSchedule(cronScheduleBuilder)
                .build();
        return cronTrigger;
    }
}
